package com.guxingke.typecho4j.widget.handle.user;

import com.guxingke.typecho4j.Typecho4jException;
import com.guxingke.typecho4j.jfinal.model.User;
import com.guxingke.typecho4j.route.DispatcherStrategy;
import com.guxingke.typecho4j.route.Route;
import com.guxingke.typecho4j.route.Routes;
import com.guxingke.typecho4j.util.MD5Util;
import com.guxingke.typecho4j.widget.WidgetHandle;
import com.guxingke.typecho4j.widget.provider.UserBaseWidget;
import com.jfinal.core.Controller;

/**
 * 更新个人登录密码的处理组件
 * 
 * @author guxingke
 *
 */
public class UpdateUserPasswordHandleWidget extends UserBaseWidget implements
		WidgetHandle {

	@Override
	public void handle(Controller controller) throws Typecho4jException {
		User user = controller.getModel(User.class);

		if (!controller.getPara("user.password").equals(
				controller.getPara("confirm"))) {
			throw new Typecho4jException(702);
		} else {
			try {
				user.set("password",
						MD5Util.EncoderPwdByMd5(controller
								.getPara("confirm")));
			} catch (Exception e) {
				throw new Typecho4jException(703);
			}
			user.update();
		}
	}

	@Override
	public void configRoute(Routes routes) {
		Route updateUserPasswordRoute = new Route("updateUserPassword",
				this.getClass(), "/admin/profile",
				DispatcherStrategy.REDIRECT);
		routes.addRoute(updateUserPasswordRoute);
	}
}
